package com.tian.aop.costtime

import android.util.Log
import com.tian.aop.tagTimeCost
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.*
import org.aspectj.lang.reflect.MethodSignature

/**
 * 方法时间消耗 切面类
 */
@Aspect
class TimeCostAspect {

//    var startTime: Long = 0

    @Pointcut("execution(@com.tian.aop.costtime.TimeCost * *(..))")
    fun methodTimeCostPoint() {
    }

//    @Before("methodTimeCostPoint()")
//    fun doBefore(joinPoint: JoinPoint) {
//        val signature: MethodSignature = joinPoint.signature as MethodSignature
//        val method = signature.method
//        Log.d(tagTimeCost, "doBefore: $method")
//        startTime = System.currentTimeMillis()
//    }
//
//    @After("methodTimeCostPoint()")
//    fun doAfter() {
//        val endTime = System.currentTimeMillis()
//        val consumeTime = endTime - startTime
//        Log.d(tagTimeCost, "开始于${startTime}，结束于$endTime, 耗时 $consumeTime ms")
//    }


    @Around("methodTimeCostPoint()")
    @Throws(Throwable::class)
    fun getMethodCostTime(joinPoint: ProceedingJoinPoint) {
        val signature: MethodSignature = joinPoint.signature as MethodSignature
        val method = signature.method
        val startTime = System.currentTimeMillis()
        joinPoint.proceed()
        val endTime = System.currentTimeMillis()
        Log.d(
            tagTimeCost,
            "${joinPoint.signature} 开始于${startTime}，结束于$endTime, 耗时 ${endTime - startTime} ms"
        )
    }
}